package genericdemo;

import java.lang.reflect.Array;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: XMQ
 * Date: 2022-06-03
 * Time: 17:15
 */
//通配符
class Message<T>{
    public T[] array;
    public Message(){
    }
    public Message(Class<T> calzz, int capacity){
        array = (T[]) Array.newInstance(calzz,capacity);
    }
    public void setArray(int pos,T val){
         array[pos] = val;
    }
    public T getArray(int pos){
        return this.array[pos];
    }
}
public class TestDemo2 {

    public static void func2(Message<? super Integer> array){
        System.out.println(array.getArray(0));
    }
    public static void func1(Message<? extends Number> array){
        System.out.println(array.getArray(0));
    }
    public static void main(String[] args) {
        Message<Integer> message = new Message<>(Integer.class,3);
        message.setArray(0,123);
        func1(message);

        Message<String> message2 = new Message<>(String.class,3);
        message.setArray(0,123);
        func2(message);

    }
    public static void func(Message<?> array){
        System.out.println(array.getArray(0));
    }
    public static void main1(String[] args) {
        Message<Integer> message = new Message<>(Integer.class,3);
        message.setArray(0,123);
        func(message);

        Message<String> message1 = new Message<>(String.class,3);
        message1.setArray(0,"nihao");
        func(message1);
    }

}
